package com.ccp.dev.qbdms.service;

import com.ccp.dev.core.basic.base.BaseService;
import com.ccp.dev.core.basic.util.ContextUtil;
import com.ccp.dev.core.basic.web.query.QueryFilter;
import com.ccp.dev.core.poi.excel.Excel;
import com.ccp.dev.core.util.BeanUtils;
import com.ccp.dev.core.util.StringUtil;
import com.ccp.dev.extend.model.ExtendSysFile;
import com.ccp.dev.qbdms.consts.ZhiLiangConst;
import com.ccp.dev.qbdms.dao.WQbdQuasuporgBaseDao;
import com.ccp.dev.qbdms.model.WQbdQuasuporgAffirm;
import com.ccp.dev.qbdms.model.WQbdQuasuporgBase;
import com.ccp.dev.qbdms.model.WQbdQuasuporgInspect;
import com.ccp.dev.qbdms.model.WQbdQuasuporgMeeting;
import com.ccp.dev.qbdms.model.WQbdQuasuporgSummary;
import com.ccp.dev.qbdms.model.WQbdQuasuporgSystem;
import com.ccp.dev.qbdms.util.BaseUtil;
import com.ccp.dev.qbdms.util.ProjectUUIDUtils;
import com.ccp.dev.qbdms.util.ReadAttachmentUtil;
import com.ccp.dev.system.model.SysOrg;
import com.ccp.dev.system.model.SysUser;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * 质量技术支撑机构 信息操作服务类
 *
 * @author y
 * @date 2020-07-08 11:17:18
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class WQbdQuasuporgBaseService extends BaseService<WQbdQuasuporgBase> {

    @Resource
    private WQbdQuasuporgBaseDao dao;
    @Resource
    private WQbdQuasuporgAffirmService wQbdQuasuporgAffirmService;
    @Resource
    private WQbdQuasuporgInspectService wQbdQuasuporgInspectService;
    @Resource
    private WQbdQuasuporgMeetingService wQbdQuasuporgMeetingService;
    @Resource
    private WQbdQuasuporgSummaryService wQbdQuasuporgSummaryService;
    @Resource
    private WQbdQuasuporgSystemService wQbdQuasuporgSystemService;

    public HSSFWorkbook exportExcel(Excel excel, QueryFilter queryFilter) {
        List<WQbdQuasuporgBase> dataList = this.dao.queryAll(queryFilter);
        int startRow = 1;
        //给每一个单元格赋值
        for (WQbdQuasuporgBase data : dataList) {
            excel.cell(startRow, 0).value(data.getId());
            //excel.cell(startRow, 1).value(data.getMc());
            //可在赋值时，根据需求进行格式化
            //excel.cell(startRow, 2).value(DateFormatUtil.formatDate(data.getRq()));
            //excel.cell(startRow, 3).value(data.getSj());
            //excel.cell(startRow, 4).value(data.getTenantId());
            startRow++;
        }
        return excel.getWorkBook();
    }

    /**
     * 保存
     *
     * @param wQbdQuasuporgBase model
     */
    public void save(WQbdQuasuporgBase wQbdQuasuporgBase) {
        List<WQbdQuasuporgAffirm> wQbdQuasuporgAffirmList = wQbdQuasuporgBase.getwQbdQuasuporgAffirmList();
        List<WQbdQuasuporgInspect> wQbdQuasuporgInspectList = wQbdQuasuporgBase.getwQbdQuasuporgInspectList();
        List<WQbdQuasuporgMeeting> wQbdQuasuporgMeetingList = wQbdQuasuporgBase.getwQbdQuasuporgMeetingList();
        List<WQbdQuasuporgSummary> wQbdQuasuporgSummaryList = wQbdQuasuporgBase.getwQbdQuasuporgSummaryList();
        List<WQbdQuasuporgSystem> wQbdQuasuporgSystemList = wQbdQuasuporgBase.getwQbdQuasuporgSystemList();
        if (StringUtil.isEmpty(wQbdQuasuporgBase.getId())) {
            //新增
            String id = ProjectUUIDUtils.getUUIDFor32();
            wQbdQuasuporgBase.setId(id);
            ZhiLiangConst.addCommonFields(wQbdQuasuporgBase);
            wQbdQuasuporgBase.setReportStatus(ZhiLiangConst.getDataInitStatusToHospital());
            if (BeanUtils.isNotEmpty(wQbdQuasuporgAffirmList)) {
                wQbdQuasuporgAffirmList.forEach(a -> {
                    a.setAffirmId(ProjectUUIDUtils.getUUIDFor32());
                    a.setAffirmRefId(id);
                });
                wQbdQuasuporgAffirmService.addList(wQbdQuasuporgAffirmList);
            }
            if (BeanUtils.isNotEmpty(wQbdQuasuporgInspectList)) {
                wQbdQuasuporgInspectList.forEach(a -> {
                    a.setInspectId(ProjectUUIDUtils.getUUIDFor32());
                    a.setInspectRefId(id);
                });
                wQbdQuasuporgInspectService.addList(wQbdQuasuporgInspectList);
            }
            if (BeanUtils.isNotEmpty(wQbdQuasuporgMeetingList)) {
                wQbdQuasuporgMeetingList.forEach(a -> {
                    a.setMeetingId(ProjectUUIDUtils.getUUIDFor32());
                    a.setMeetingRefId(id);
                });
                wQbdQuasuporgMeetingService.addList(wQbdQuasuporgMeetingList);
            }
            if (BeanUtils.isNotEmpty(wQbdQuasuporgSummaryList)) {
                wQbdQuasuporgSummaryList.forEach(a -> {
                    a.setSummaryId(ProjectUUIDUtils.getUUIDFor32());
                    a.setSummaryRefId(id);
                });
                wQbdQuasuporgSummaryService.addList(wQbdQuasuporgSummaryList);
            }
            if (BeanUtils.isNotEmpty(wQbdQuasuporgSystemList)) {
                wQbdQuasuporgSystemList.forEach(a -> {
                    a.setSystemId(ProjectUUIDUtils.getUUIDFor32());
                    a.setSystemRefId(id);
                });
                wQbdQuasuporgSystemService.addList(wQbdQuasuporgSystemList);
            }
            this.add(wQbdQuasuporgBase);
        } else {
            //编辑
            String id = wQbdQuasuporgBase.getId();
            ZhiLiangConst.updateCommonFields(wQbdQuasuporgBase);
            List<WQbdQuasuporgAffirm> oldAffirmList = wQbdQuasuporgAffirmService.queryByRefId(id);
            /*
             * 1.用老数据过滤新数据   不包含的就是要删除的数据
             * 2.用新数据过滤老数据   包含的就是老数据，不包含的就是要添加的数据
             */
            List<WQbdQuasuporgAffirm> delAffirmList = oldAffirmList.stream().filter(a -> wQbdQuasuporgAffirmList.stream().noneMatch(b -> (StringUtil.isEmpty(a.getAffirmId()) ? "" : a.getAffirmId()).equals((StringUtil.isEmpty(b.getAffirmId()) ? "" : b.getAffirmId())))).collect(Collectors.toList());

            List<WQbdQuasuporgAffirm> updateAffirmList = wQbdQuasuporgAffirmList.stream().filter(a -> oldAffirmList.stream().anyMatch(b -> (StringUtil.isEmpty(a.getAffirmId()) ? "" : a.getAffirmId()).equals((StringUtil.isEmpty(b.getAffirmId()) ? "" : b.getAffirmId())))).collect(Collectors.toList());

            List<WQbdQuasuporgAffirm> addAffirmList = wQbdQuasuporgAffirmList.stream().filter(a -> oldAffirmList.stream().noneMatch(b -> (StringUtil.isEmpty(a.getAffirmId()) ? "" : a.getAffirmId()).equals((StringUtil.isEmpty(b.getAffirmId()) ? "" : b.getAffirmId())))).collect(Collectors.toList());

            if (BeanUtils.isNotEmpty(delAffirmList)) {
                wQbdQuasuporgAffirmService.delByIdArray(delAffirmList.stream().map(WQbdQuasuporgAffirm::getAffirmId).collect(Collectors.toList()).toArray(new String[]{}));
            }

            if (BeanUtils.isNotEmpty(updateAffirmList)) {
                updateAffirmList.forEach(a -> {
                    wQbdQuasuporgAffirmService.update(a);
                });
            }
            if (BeanUtils.isNotEmpty(addAffirmList)) {
                addAffirmList.forEach(a -> {
                    a.setAffirmId(ProjectUUIDUtils.getUUIDFor32());
                    a.setAffirmRefId(id);
                });
                wQbdQuasuporgAffirmService.addList(addAffirmList);
            }

            List<WQbdQuasuporgInspect> oldInspectList = wQbdQuasuporgInspectService.queryByRefId(id);
            /*
             * 1.用老数据过滤新数据   不包含的就是要删除的数据
             * 2.用新数据过滤老数据   包含的就是老数据，不包含的就是要添加的数据
             */
            List<WQbdQuasuporgInspect> delInspectList = oldInspectList.stream().filter(a -> wQbdQuasuporgInspectList.stream().noneMatch(b -> (StringUtil.isEmpty(a.getInspectId()) ? "" : a.getInspectId()).equals((StringUtil.isEmpty(b.getInspectId()) ? "" : b.getInspectId())))).collect(Collectors.toList());

            List<WQbdQuasuporgInspect> updateInspectList = wQbdQuasuporgInspectList.stream().filter(a -> oldInspectList.stream().anyMatch(b -> (StringUtil.isEmpty(a.getInspectId()) ? "" : a.getInspectId()).equals((StringUtil.isEmpty(b.getInspectId()) ? "" : b.getInspectId())))).collect(Collectors.toList());

            List<WQbdQuasuporgInspect> addInspectList = wQbdQuasuporgInspectList.stream().filter(a -> oldInspectList.stream().noneMatch(b -> (StringUtil.isEmpty(a.getInspectId()) ? "" : a.getInspectId()).equals((StringUtil.isEmpty(b.getInspectId()) ? "" : b.getInspectId())))).collect(Collectors.toList());

            if (BeanUtils.isNotEmpty(delInspectList)) {
                wQbdQuasuporgInspectService.delByIdArray(delInspectList.stream().map(WQbdQuasuporgInspect::getInspectId).collect(Collectors.toList()).toArray(new String[]{}));
            }

            if (BeanUtils.isNotEmpty(updateInspectList)) {
                updateInspectList.forEach(a -> {
                    wQbdQuasuporgInspectService.update(a);
                });
            }
            if (BeanUtils.isNotEmpty(addInspectList)) {
                addInspectList.forEach(a -> {
                    a.setInspectId(ProjectUUIDUtils.getUUIDFor32());
                    a.setInspectRefId(id);
                });
                wQbdQuasuporgInspectService.addList(addInspectList);
            }

            List<WQbdQuasuporgMeeting> oldMeetingList = wQbdQuasuporgMeetingService.queryByRefId(id);
            /*
             * 1.用老数据过滤新数据    不包含的就是要删除的数据
             * 2.用新数据过滤老数据    包含的就是老数据，不包含的就是要添加的数据
             */
            List<WQbdQuasuporgMeeting> delMeetingList = oldMeetingList.stream().filter(a -> wQbdQuasuporgMeetingList.stream().noneMatch(b -> (StringUtil.isEmpty(a.getMeetingId()) ? "" : a.getMeetingId()).equals((StringUtil.isEmpty(b.getMeetingId()) ? "" : b.getMeetingId())))).collect(Collectors.toList());

            List<WQbdQuasuporgMeeting> updateIMeetingList = wQbdQuasuporgMeetingList.stream().filter(a -> oldMeetingList.stream().anyMatch(b -> (StringUtil.isEmpty(a.getMeetingId()) ? "" : a.getMeetingId()).equals((StringUtil.isEmpty(b.getMeetingId()) ? "" : b.getMeetingId())))).collect(Collectors.toList());

            List<WQbdQuasuporgMeeting> addMeetingList = wQbdQuasuporgMeetingList.stream().filter(a -> oldMeetingList.stream().noneMatch(b -> (StringUtil.isEmpty(a.getMeetingId()) ? "" : a.getMeetingId()).equals((StringUtil.isEmpty(b.getMeetingId()) ? "" : b.getMeetingId())))).collect(Collectors.toList());

            if (BeanUtils.isNotEmpty(delMeetingList)) {
                wQbdQuasuporgMeetingService.delByIdArray(delMeetingList.stream().map(WQbdQuasuporgMeeting::getMeetingId).collect(Collectors.toList()).toArray(new String[]{}));
            }

            if (BeanUtils.isNotEmpty(updateIMeetingList)) {
                updateIMeetingList.forEach(a -> {
                    wQbdQuasuporgMeetingService.update(a);
                });
            }
            if (BeanUtils.isNotEmpty(addMeetingList)) {
                addMeetingList.forEach(a -> {
                    a.setMeetingId(ProjectUUIDUtils.getUUIDFor32());
                    a.setMeetingRefId(id);
                });
                wQbdQuasuporgMeetingService.addList(addMeetingList);
            }
            List<WQbdQuasuporgSummary> oldSummaryList = wQbdQuasuporgSummaryService.queryByRefId(id);
            /*
             * 1.用老数据过滤新数据   不包含的就是要删除的数据
             * 2.用新数据过滤老数据   包含的就是老数据，不包含的就是要添加的数据
             */
            List<WQbdQuasuporgSummary> delSummaryList = oldSummaryList.stream().filter(a -> wQbdQuasuporgSummaryList.stream().noneMatch(b -> (StringUtil.isEmpty(a.getSummaryId()) ? "" : a.getSummaryId()).equals((StringUtil.isEmpty(b.getSummaryId()) ? "" : b.getSummaryId())))).collect(Collectors.toList());

            List<WQbdQuasuporgSummary> updateISummaryList = wQbdQuasuporgSummaryList.stream().filter(a -> oldSummaryList.stream().anyMatch(b -> (StringUtil.isEmpty(a.getSummaryId()) ? "" : a.getSummaryId()).equals((StringUtil.isEmpty(b.getSummaryId()) ? "" : b.getSummaryId())))).collect(Collectors.toList());

            List<WQbdQuasuporgSummary> addSummaryList = wQbdQuasuporgSummaryList.stream().filter(a -> oldSummaryList.stream().noneMatch(b -> (StringUtil.isEmpty(a.getSummaryId()) ? "" : a.getSummaryId()).equals((StringUtil.isEmpty(b.getSummaryId()) ? "" : b.getSummaryId())))).collect(Collectors.toList());

            if (BeanUtils.isNotEmpty(delSummaryList)) {
                wQbdQuasuporgSummaryService.delByIdArray(delSummaryList.stream().map(WQbdQuasuporgSummary::getSummaryId).collect(Collectors.toList()).toArray(new String[]{}));
            }

            if (BeanUtils.isNotEmpty(updateISummaryList)) {
                updateISummaryList.forEach(a -> {
                    wQbdQuasuporgSummaryService.update(a);
                });
            }
            if (BeanUtils.isNotEmpty(addSummaryList)) {
                addSummaryList.forEach(a -> {
                    a.setSummaryId(ProjectUUIDUtils.getUUIDFor32());
                    a.setSummaryRefId(id);
                });
                wQbdQuasuporgSummaryService.addList(addSummaryList);
            }
            List<WQbdQuasuporgSystem> oldSystemList = wQbdQuasuporgSystemService.queryByRefId(id);
            /*
             * 1.用老数据过滤新数据   不包含的就是要删除的数据
             * 2.用新数据过滤老数据   包含的就是老数据，不包含的就是要添加的数据
             */
            List<WQbdQuasuporgSystem> delSystemList = oldSystemList.stream().filter(a -> wQbdQuasuporgSystemList.stream().noneMatch(b -> (StringUtil.isEmpty(a.getSystemId()) ? "" : a.getSystemId()).equals((StringUtil.isEmpty(b.getSystemId()) ? "" : b.getSystemId())))).collect(Collectors.toList());

            List<WQbdQuasuporgSystem> updateISystemList = wQbdQuasuporgSystemList.stream().filter(a -> oldSystemList.stream().anyMatch(b -> (StringUtil.isEmpty(a.getSystemId()) ? "" : a.getSystemId()).equals((StringUtil.isEmpty(b.getSystemId()) ? "" : b.getSystemId())))).collect(Collectors.toList());

            List<WQbdQuasuporgSystem> addSystemList = wQbdQuasuporgSystemList.stream().filter(a -> oldSystemList.stream().noneMatch(b -> (StringUtil.isEmpty(a.getSystemId()) ? "" : a.getSystemId()).equals((StringUtil.isEmpty(b.getSystemId()) ? "" : b.getSystemId())))).collect(Collectors.toList());

            if (BeanUtils.isNotEmpty(delSystemList)) {
                wQbdQuasuporgSystemService.delByIdArray(delSystemList.stream().map(WQbdQuasuporgSystem::getSystemId).collect(Collectors.toList()).toArray(new String[]{}));
            }

            if (BeanUtils.isNotEmpty(updateISystemList)) {
                updateISystemList.forEach(a -> {
                    wQbdQuasuporgSystemService.update(a);
                });
            }
            if (BeanUtils.isNotEmpty(addSystemList)) {
                addSystemList.forEach(a -> {
                    a.setSystemId(ProjectUUIDUtils.getUUIDFor32());
                    a.setSystemRefId(id);
                });
                wQbdQuasuporgSystemService.addList(addSystemList);
            }
            this.update(wQbdQuasuporgBase);
        }
    }

    /**
     * 根据id获取包含子表的所有数据
     *
     * @param id 主表主键
     * @return model
     */
    public WQbdQuasuporgBase getAllData(String id) {
        if (StringUtil.isEmpty(id)) {
            return null;
        }
        WQbdQuasuporgBase wQbdQuasuporgBase = this.getOneById(id);
        if (BeanUtils.isEmpty(wQbdQuasuporgBase)) {
            return null;
        }
        wQbdQuasuporgBase.setwQbdQuasuporgAffirmList(wQbdQuasuporgAffirmService.queryByRefId(id));
        wQbdQuasuporgBase.setwQbdQuasuporgInspectList(wQbdQuasuporgInspectService.queryByRefId(id));
        wQbdQuasuporgBase.setwQbdQuasuporgMeetingList(wQbdQuasuporgMeetingService.queryByRefId(id));
        wQbdQuasuporgBase.setwQbdQuasuporgSummaryList(wQbdQuasuporgSummaryService.queryByRefId(id));
        wQbdQuasuporgBase.setwQbdQuasuporgSystemList(wQbdQuasuporgSystemService.queryByRefId(id));
        return wQbdQuasuporgBase;
    }

    /**
     * 根据主表id 删除数据
     *
     * @param ids 删除数据
     */
    public int delByIds(String[] ids) {
        for (String id : ids) {
            wQbdQuasuporgAffirmService.delByRefId(id);
            wQbdQuasuporgInspectService.delByRefId(id);
            wQbdQuasuporgMeetingService.delByRefId(id);
            wQbdQuasuporgSummaryService.delByRefId(id);
            wQbdQuasuporgSystemService.delByRefId(id);
        }
        return this.delByIdArray(ids);
    }

    /**
     * 查询上报流程的业务数据
     *
     * @param dataId    数据id
     * @param tableName tableName
     * @return list
     */
    public List<WQbdQuasuporgBase> reportList(String dataId, String tableName) {
        return dao.reportList(dataId, tableName);
    }

    /**
     * 导出用查询
     *
     * @param queryFilter 查询参数
     * @return list
     */
    public List<WQbdQuasuporgBase> queryAll(QueryFilter queryFilter) {
        return dao.queryAll(queryFilter);
    }

    /**
     * 导入新增
     *
     * @param list 数据集
     * @throws Exception 异常
     */
    public void addImportData(List<WQbdQuasuporgBase> list) throws Exception {
        List<WQbdQuasuporgAffirm> wQbdQuasuporgAffirmList = new ArrayList<>();
        List<WQbdQuasuporgInspect> wQbdQuasuporgInspectList = new ArrayList<>();
        List<WQbdQuasuporgMeeting> wQbdQuasuporgMeetingList = new ArrayList<>();
        List<WQbdQuasuporgSummary> wQbdQuasuporgSummaryList = new ArrayList<>();
        List<WQbdQuasuporgSystem> wQbdQuasuporgSystemList = new ArrayList<>();
        String path = BaseUtil.getCurrentTenOrgCodePath();
        Date date = new Date();
        SysUser sysUser = (SysUser) ContextUtil.getCurrentUser();
        String userId = sysUser.getUserId();
        String tenantId = ContextUtil.getCurrentUserTenantId();
        SysOrg sysOrg = (SysOrg) ContextUtil.getCurrentOrg();
        String code = sysOrg.getCode();
        String orgName = sysOrg.getOrgName();
        String fillMan = sysUser.getFullName();
        String account = sysUser.getAccount();
        list.forEach(a -> {
            String mainId = ProjectUUIDUtils.getUUIDFor32();
            a.setId(mainId);
            a.setReportStatus(ZhiLiangConst.getDataInitStatusToHospital());
            ZhiLiangConst.addCommonFields(a);
            a.setAllowEdit(true);
            if (StringUtil.isEmpty(a.getBelongdeptname())) {
                a.setBelongdeptname(path);
            }
            if (StringUtil.isNotEmpty(a.getContactsInfo())) {
                a.setContactsInfo(a.getContactsInfo().replace(",", ""));
            }
            List<WQbdQuasuporgAffirm> wQbdQuasuporgAffirmLists = a.getwQbdQuasuporgAffirmList();
            if (null != wQbdQuasuporgAffirmLists && wQbdQuasuporgAffirmLists.size() > 0) {
                wQbdQuasuporgAffirmLists.forEach(b -> {
                    b.setAffirmYear(b.getAffirmYear().replace(",", ""));
                    b.setAffirmRefId(mainId);
                    b.setAffirmId(ProjectUUIDUtils.getUUIDFor32());
                    b.setCreateTime(date);
                    b.setCreateBy(userId);
                    b.setTenantId(tenantId);
                });
                wQbdQuasuporgAffirmList.addAll(wQbdQuasuporgAffirmLists);
            }
            List<WQbdQuasuporgInspect> wQbdQuasuporgInspectLists = a.getwQbdQuasuporgInspectList();
            if (null != wQbdQuasuporgInspectLists && wQbdQuasuporgInspectLists.size() > 0) {
                wQbdQuasuporgInspectLists.forEach(b -> {
                    b.setInspectYear(b.getInspectYear().replace(",", ""));
                    b.setInspectRefId(mainId);
                    b.setInspectId(ProjectUUIDUtils.getUUIDFor32());
                    b.setCreateTime(date);
                    b.setCreateBy(userId);
                    b.setTenantId(tenantId);
                });
                wQbdQuasuporgInspectList.addAll(wQbdQuasuporgInspectLists);
            }
            List<WQbdQuasuporgMeeting> wQbdQuasuporgMeetingLists = a.getwQbdQuasuporgMeetingList();
            if (null != wQbdQuasuporgMeetingLists && wQbdQuasuporgMeetingLists.size() > 0) {
                wQbdQuasuporgMeetingLists.forEach(b -> {
                    b.setMeetingYear(b.getMeetingYear().replace(",", ""));
                    b.setMeetingRefId(mainId);
                    b.setMeetingId(ProjectUUIDUtils.getUUIDFor32());
                    b.setCreateTime(date);
                    b.setCreateBy(userId);
                    b.setTenantId(tenantId);
                });
                wQbdQuasuporgMeetingList.addAll(wQbdQuasuporgMeetingLists);
            }
            List<WQbdQuasuporgSummary> wQbdQuasuporgSummaryLists = a.getwQbdQuasuporgSummaryList();
            if (null != wQbdQuasuporgSummaryLists && wQbdQuasuporgSummaryLists.size() > 0) {
                wQbdQuasuporgSummaryLists.forEach(b -> {
                    b.setSummaryYear(b.getSummaryYear().replace(",", ""));
                    b.setSummaryRefId(mainId);
                    b.setSummaryId(ProjectUUIDUtils.getUUIDFor32());
                    b.setCreateTime(date);
                    b.setCreateBy(userId);
                    b.setTenantId(tenantId);
                });
                wQbdQuasuporgSummaryList.addAll(wQbdQuasuporgSummaryLists);
            }
            List<WQbdQuasuporgSystem> wQbdQuasuporgSystemLists = a.getwQbdQuasuporgSystemList();
            if (null != wQbdQuasuporgSystemLists && wQbdQuasuporgSystemLists.size() > 0) {
                wQbdQuasuporgSystemLists.forEach(b -> {
                    b.setSystemYear(b.getSystemYear().replace(",", ""));
                    b.setSystemRefId(mainId);
                    b.setSystemId(ProjectUUIDUtils.getUUIDFor32());
                    b.setCreateTime(date);
                    b.setCreateBy(userId);
                    b.setTenantId(tenantId);
                });
                wQbdQuasuporgSystemList.addAll(wQbdQuasuporgSystemLists);
            }
        });

        if (!list.isEmpty()) {
            dao.addList(list);
            if (!wQbdQuasuporgAffirmList.isEmpty()) {
                wQbdQuasuporgAffirmService.addList(wQbdQuasuporgAffirmList);
            }
            if (!wQbdQuasuporgInspectList.isEmpty()) {
                wQbdQuasuporgInspectService.addList(wQbdQuasuporgInspectList);
            }
            if (!wQbdQuasuporgMeetingList.isEmpty()) {
                wQbdQuasuporgMeetingService.addList(wQbdQuasuporgMeetingList);
            }
            if (!wQbdQuasuporgSummaryList.isEmpty()) {
                wQbdQuasuporgSummaryService.addList(wQbdQuasuporgSummaryList);
            }
            if (!wQbdQuasuporgSystemList.isEmpty()) {
                wQbdQuasuporgSystemService.addList(wQbdQuasuporgSystemList);
            }

        }
    }


    /**
     * 根据主表id查询主表加子表数据
     *
     * @param id 主表id
     * @return
     */
    public WQbdQuasuporgBase getParentAndChilden(String id) {
        return dao.getParentAndChilden(id);
    }

    /**
     * 保存接口传输的数据
     *
     * @param wQbdQuasuporgBases 主表model,有子表的包含子表数据
     * @return
     */
    public int saveAll(List<WQbdQuasuporgBase> wQbdQuasuporgBases,List<ExtendSysFile> extendSysFileList){
        ReadAttachmentUtil.getEntityData(wQbdQuasuporgBases,extendSysFileList);
        List<WQbdQuasuporgAffirm> wQbdQuasuporgAffirmList = new ArrayList<>();
        List<WQbdQuasuporgInspect> wQbdQuasuporgInspectList = new ArrayList<>();
        List<WQbdQuasuporgMeeting> wQbdQuasuporgMeetingList = new ArrayList<>();
        List<WQbdQuasuporgSummary> wQbdQuasuporgSummaryList = new ArrayList<>();
        List<WQbdQuasuporgSystem> wQbdQuasuporgSystemList = new ArrayList<>();
        for (WQbdQuasuporgBase wQbdQuasuporgBase : wQbdQuasuporgBases) {
            if (StringUtil.isEmpty(wQbdQuasuporgBase.getFilldeptHospitalId())) {
                wQbdQuasuporgBase.setBelongdeptname(BaseUtil.getCurrentTenOrgCodePath(wQbdQuasuporgBase.getFilldeptid()));
            } else {
                wQbdQuasuporgBase.setBelongdeptname(BaseUtil.getCurrentTenOrgCodePath(wQbdQuasuporgBase.getFilldeptHospitalId()));
            }
            List<WQbdQuasuporgAffirm> wQbdQuasuporgAffirms = wQbdQuasuporgBase.getwQbdQuasuporgAffirmList();
            List<WQbdQuasuporgInspect> wQbdQuasuporgInspects = wQbdQuasuporgBase.getwQbdQuasuporgInspectList();
            List<WQbdQuasuporgMeeting> wQbdQuasuporgMeetings = wQbdQuasuporgBase.getwQbdQuasuporgMeetingList();
            List<WQbdQuasuporgSummary> wQbdQuasuporgSummarys = wQbdQuasuporgBase.getwQbdQuasuporgSummaryList();
            List<WQbdQuasuporgSystem> wQbdQuasuporgSystems = wQbdQuasuporgBase.getwQbdQuasuporgSystemList();
            // 子表数据
            if (BeanUtils.isNotEmpty(wQbdQuasuporgAffirms)) {
                wQbdQuasuporgAffirmList.addAll(wQbdQuasuporgAffirms);
            }
            if (BeanUtils.isNotEmpty(wQbdQuasuporgInspects)) {
                wQbdQuasuporgInspectList.addAll(wQbdQuasuporgInspects);
            }
            if (BeanUtils.isNotEmpty(wQbdQuasuporgMeetings)) {
                wQbdQuasuporgMeetingList.addAll(wQbdQuasuporgMeetings);
            }
            if (BeanUtils.isNotEmpty(wQbdQuasuporgSummarys)) {
                wQbdQuasuporgSummaryList.addAll(wQbdQuasuporgSummarys);
            }
            if (BeanUtils.isNotEmpty(wQbdQuasuporgSystems)) {
                wQbdQuasuporgSystemList.addAll(wQbdQuasuporgSystems);
            }
        }
        // 子表数据
        if (BeanUtils.isNotEmpty(wQbdQuasuporgAffirmList)) {
            wQbdQuasuporgAffirmService.addList(wQbdQuasuporgAffirmList);
        }
        if (BeanUtils.isNotEmpty(wQbdQuasuporgInspectList)) {
            wQbdQuasuporgInspectService.addList(wQbdQuasuporgInspectList);
        }
        if (BeanUtils.isNotEmpty(wQbdQuasuporgMeetingList)) {
            wQbdQuasuporgMeetingService.addList(wQbdQuasuporgMeetingList);
        }
        if (BeanUtils.isNotEmpty(wQbdQuasuporgSummaryList)) {
            wQbdQuasuporgSummaryService.addList(wQbdQuasuporgSummaryList);
        }
        if (BeanUtils.isNotEmpty(wQbdQuasuporgSystemList)) {
            wQbdQuasuporgSystemService.addList(wQbdQuasuporgSystemList);
        }
        // 主表数据
        return this.addList(wQbdQuasuporgBases);
    }
}
